using System;
using System.Collections.Generic;
using System.Text;
using Barrage;
using Spacedash.Logic;
using Barrage.Items;
using Spacedash.Game.Scenes;
using SlimDX;
using Barrage.Render.Passes;
using Barrage.Scenes;
using Barrage.Global;
using Barrage.Render;
using SlimDX.Direct3D9;
using Spacedash.Logic.Scenario;
using Spacedash.Logic.Scenario.Cells;

namespace Spacedash.Game.Subgames
{
    class CreditsSection : GameSection
    {
        public override int Id
        {
            get { return (int)GameState.Credits; }
        }

        protected Scenario scenario;
        public Scenario Scenario { get { return scenario; } }

        protected Postprocess blurProcess = null;

        protected CreditsScene creditsScene;

        public CreditsSection(SpacedashGame parent)
            : base(parent)
        {
            CellManager.ClearPatterns();
            CellManager.LoadPatterns(Race.PATTERNS_DIR + Race.PATTERN_FILE);
            scenario = BitmapScenarioLoader.LoadScenario(Spacedash.Logic.Scenario.Scenario.SCENARIO_DIR + "Credits" + Spacedash.Logic.Scenario.Scenario.SCENARIO_EXT);
            
            
        }

        bool secondFrame = false;
        public override void MainLoop()
        {
            // If it's the second frame
            if (secondFrame) scenario.ClearChangedCells();
            secondFrame = true;
            
            if (Keyboard.Instance.KeyReleased(SlimDX.DirectInput.Key.Escape) || Gamepad.AnyButtonReleased(SlimDX.XInput.GamepadButtonFlags.Back))
                parent.NextGameSectionId = (int)GameState.MainMenu;            
        }

        protected override void initRenderProcesses()
        {

                RenderPass scenePass;
                if (Engine.Config[GameConfig.ANTIALIAS].Equals("true"))
                    scenePass = new AntialiasRenderPass();
                else
                    scenePass = new RenderPass();
                scenePass.MustClear = true;
                scenePass.BackgroundColor = new Color4(1, 0.8f, 0.8f, 0.8f);
                scenePass.addSource(creditsScene, creditsScene.Camera);

                // Second pass: Postprocess the antialiased scene with a radial blur
                PostprocessScene ppScene = new PostprocessScene();
                blurProcess = new Postprocess(scenePass.RenderTarget, "RadialBlur.fx");
            // Blur amount (updates each frame)
                blurProcess.Material.FloatParameters.Add(0.015f);
            // Desaturation weight
                blurProcess.Material.FloatParameters.Add(42);
            // Min saturation
                blurProcess.Material.FloatParameters.Add(0);
            // Original Pixel Weight
                blurProcess.Material.FloatParameters.Add(24);
                blurProcess.Material.Vector4Parameters.Add(new Vector4(1, 0.05f, 0, 0));
                ppScene.Add(blurProcess);                

                RenderProcess renderProcess = new RenderProcess((Texture)null);
                renderProcess.FinalRenderPass.MustClear = false;
                renderProcess.FinalRenderPass.DepthBuffer = null;
                renderProcess.FinalRenderPass.ZCull = false;

                renderProcess.addIntermediatePass(scenePass);

                // The result of the blur postprocess can be rendered directly to backbuffer
                renderProcess.addSource(ppScene);

                AddRenderProcess(renderProcess);
        }

        protected override void initScenes()
        {
            creditsScene = new CreditsScene(scenario);
            AddScene(creditsScene);
        }

        public override void Dispose()
        {
            scenario = null;
            creditsScene = null;
            blurProcess = null;
            base.Dispose();
        }
    }
}
